cmake_minimum_required(VERSION 3.10)
project(libparser C)
set(CMAKE_C_STANDARD 99)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(PCAP REQUIRED libpcap)



set(NGHTTP2_INCLUDE_DIRS /usr/local/include/nghttp2)
set(NGHTTP2_LIBRARIES /usr/local/lib/libnghttp2.a)



# 设置共享库编译标志（根据你的 Makefile 中的选项）
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -D_GNU_SOURCE -std=gnu99 -fno-strict-aliasing -g -fPIC -pthread")
set(SHARED_FLAGS "-shared")

# 设置 include 路径
include_directories(.. ../thirdparty)
include_directories(${PCAP_INCLUDE_DIRS} ${NGHTTP2_INCLUDE_DIRS} ${GLIB2_INCLUDE_DIRS})

file(GLOB SOURCES "*.c")

# 为每个 .c 文件创建一个共享库 .so 文件
foreach(SOURCE_FILE ${SOURCES})
    # 获取源文件的名称（不带扩展名）
    get_filename_component(LIB_NAME ${SOURCE_FILE} NAME_WE)
    
    # 创建共享库 .so
    add_library(${LIB_NAME} SHARED ${SOURCE_FILE})
    
    # 链接需要的库（如果有）
    target_link_libraries(${LIB_NAME} ${PCAP_LIBRARIES} ${NGHTTP2_LIBRARIES} ${GLIB2_LIBRARIES})
    
    # 添加编译选项
    target_compile_options(${LIB_NAME} PRIVATE ${SHARED_FLAGS})
endforeach()

#install(TARGETS ${LIB_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/parsers)